/**
 * m3d源码场景 请勿下载后售卖
 * gitee:https://gitee.com/m3d
 * b站:https://www.bilibili.com/video/BV1yG4y1m7dF/
 */
Cesium.Ion.defaultAccessToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJjMzU2ZTQyYy1iOTU5LTQ5MDQtOGNkNC0yYzcxMTI1ZDJiZGQiLCJpZCI6NzY1OTcsImlhdCI6MTYzOTU2MDcwOH0.kbWigipGD6l2OPBGpnkkN6dzp8NuNjoHNNM1NF4gaIo';
let viewer;
let imagerLayer;
let googleTileset;
let snowEffect;
let rainEffect;
// 默认视角
Cesium.Camera.DEFAULT_VIEW_RECTANGLE = Cesium.Rectangle.fromDegrees(90, -20, 110, 90);
/**
 * 初始化viewer
 */
const initViewer = () => {
    viewer = new Cesium.Viewer('sceneContainer', {
        infoBox: false,
        shouldAnimate: false,
        vrButton: false,
        geocoder: true,
        homeButton: false,
        sceneModePicker: false,
        baseLayerPicker: false,
        navigationHelpButton: false,
        animation: false,
        timeline: true,
        fullscreenButton: false,
    });

    viewer._cesiumWidget._creditContainer.style.display = "none";
    viewer.resolutionScale = 1.2;
    viewer.scene.msaaSamples = 4;
    viewer.postProcessStages.fxaa.enabled = true;
    viewer.scene.globe.depthTestAgainstTerrain = true;
    viewer.scene.debugShowFramesPerSecond = true;
    viewer.scene.globe.enableLighting = true;
    viewer.scene.fog.minimumBrightness = 0.5;
    viewer.scene.fog.density = 2.0e-4 * 1.2;
    viewer.scene.globe.atmosphereLightIntensity = 20;
    viewer.scene.globe.atmosphereBrightnessShift = -0.01;
    imagerLayer = viewer.imageryLayers.get(0);

    viewer.scene.highDynamicRange = false;
    viewer.scene.postProcessStages.bloom.enabled = false;
    viewer.scene.postProcessStages.bloom.uniforms.contrast = 119;
    viewer.scene.postProcessStages.bloom.uniforms.brightness = -0.4;
    viewer.scene.postProcessStages.bloom.uniforms.glowOnly = false;
    viewer.scene.postProcessStages.bloom.uniforms.delta = 0.9;
    viewer.scene.postProcessStages.bloom.uniforms.sigma = 3.78;
    viewer.scene.postProcessStages.bloom.uniforms.stepSize = 5;
    viewer.scene.postProcessStages.bloom.uniforms.isSelected = false;

    viewer.scene.postProcessStages.ambientOcclusion.enabled = false;
    viewer.scene.postProcessStages.ambientOcclusion.uniforms.intensity = 1.5;
    viewer.scene.postProcessStages.ambientOcclusion.uniforms.bias = 0.4;
    viewer.scene.postProcessStages.ambientOcclusion.uniforms.lengthCap = 0.45;
    viewer.scene.postProcessStages.ambientOcclusion.uniforms.stepSize = 1.8;
    viewer.scene.postProcessStages.ambientOcclusion.uniforms.blurStepSize = 1.0;
}

/**
 * 初始化场景
 */
const initScene = async () => {

    try {
        googleTileset = await Cesium.createGooglePhotorealistic3DTileset(undefined, {
            customShader: new Cesium.CustomShader({
                uniforms: {
                    u_lightColor: {
                        type: Cesium.UniformType.VEC3,
                        value: new Cesium.Cartesian3(1, 1, 1),
                    },
                    u_snowAlpha: {
                        type: Cesium.UniformType.FLOAT,
                        value: 0.,
                    },
                    u_rainAlpha: {
                        type: Cesium.UniformType.FLOAT,
                        value: 0.,
                    },
                },
                fragmentShaderText: `
                #define MAX_RADIUS 2
                // Set to 1 to hash twice. Slower, but less patterns.
                #define DOUBLE_HASH 0
                // Hash functions shamefully stolen from:
                // https://www.shadertoy.com/view/4djSRW
                #define HASHSCALE1 .1031
                #define HASHSCALE3 vec3(.1031, .1030, .0973)
                float hash12(vec2 p)
                {
                    vec3 p3  = fract(vec3(p.xyx) * HASHSCALE1);
                    p3 += dot(p3, p3.yzx + 19.19);
                    return fract((p3.x + p3.y) * p3.z);
                }
                vec2 hash22(vec2 p)
                {
                    vec3 p3 = fract(vec3(p.xyx) * HASHSCALE3);
                    p3 += dot(p3, p3.yzx+19.19);
                    return fract((p3.xx+p3.yz)*p3.zy);

                }
                void fragmentMain(FragmentInput fsInput, inout czm_modelMaterial material) {
                    vec3 positionEC = fsInput.attributes.positionEC;
                    vec3 positionMC = fsInput.attributes.positionMC;
                    vec2 uv = fsInput.attributes.texCoord_0 * 500.;
                    vec3 pos_dx = dFdx(positionEC);
                    vec3 pos_dy = dFdy(positionEC);
                    vec3 normalEC = normalize(cross(pos_dx, pos_dy));
                    vec4 positionWC = normalize(czm_inverseView * vec4(positionEC,1.0));
                    vec3 normalWC = normalize(czm_inverseViewRotation * normalEC);
                    float time = czm_frameNumber / 60.0;
                    vec2 p0 = floor(uv);
                    vec2 circles = vec2(0.);
                    for (int j = -MAX_RADIUS; j <= MAX_RADIUS; ++j)
                    {
                        for (int i = -MAX_RADIUS; i <= MAX_RADIUS; ++i)
                        {
                            vec2 pi = p0 + vec2(i, j);
                            #if DOUBLE_HASH
                            vec2 hsh = hash22(pi);
                            #else
                            vec2 hsh = pi;
                            #endif
                            vec2 p = pi + hash22(hsh);

                            float t = fract(0.3*time + hash12(hsh));
                            vec2 v = p - uv;
                            float d = length(v) - (float(MAX_RADIUS) + 1.)*t;

                            float h = 1e-3;
                            float d1 = d - h;
                            float d2 = d + h;
                            float p1 = sin(31.*d1) * smoothstep(-0.6, -0.3, d1) * smoothstep(0., -0.3, d1);
                            float p2 = sin(31.*d2) * smoothstep(-0.6, -0.3, d2) * smoothstep(0., -0.3, d2);
                            circles += 0.5 * normalize(v) * ((p2 - p1) / (2. * h) * (1. - t) * (1. - t));
                        }
                    }
                    circles /= float((MAX_RADIUS*2+1)*(MAX_RADIUS*2+1));
                    vec3 n = vec3(circles, sqrt(1. - dot(circles, circles)));
                    material.diffuse = mix(material.diffuse, vec3(1.0) , u_snowAlpha * smoothstep(0., .5, dot(positionWC.xyz, normalWC)));
                    material.diffuse = mix(material.diffuse, vec3((n * vec3(1.2)).r) , u_rainAlpha * smoothstep(0., .5, dot(positionWC.xyz, normalWC)));
                    material.diffuse *= min(max(0.0, dot(normalEC, czm_sunDirectionEC) * 1.0) + u_lightColor, 1.0);


                }
                `
            })
        });
        viewer.scene.primitives.add(googleTileset);
    } catch (error) {
        console.log(`Error loading Photorealistic 3D Tiles tileset.
        ${error}`);
    }

    snowEffect = new Cesium.PostProcessStage({
        fragmentShader: `
        precision highp float;
        uniform sampler2D colorTexture;
        uniform sampler2D depthTexture;
        in vec2 v_textureCoordinates;
        float time;
        #define HASHSCALE1 .1031
        #define HASHSCALE3 vec3(.1031, .1030, .0973)
        #define HASHSCALE4 vec3(.1031, .1030, .0973, .1099)
        float SIZE_RATE = 0.1;
        float XSPEED = 0.2;
        float YSPEED = 0.5;
        float LAYERS = 10.;
        float Hash11(float p)
        {
            vec3 p3  = fract(vec3(p) * HASHSCALE1);
            p3 += dot(p3, p3.yzx + 19.19);
            return fract((p3.x + p3.y) * p3.z); 
        }
        vec2 Hash22(vec2 p)
        {
            vec3 p3 = fract(vec3(p.xyx) * HASHSCALE3);
            p3 += dot(p3, p3.yzx+19.19);
            return fract((p3.xx+p3.yz)*p3.zy);
        }
        vec2 Rand22(vec2 co)
        {
            float x = fract(sin(dot(co.xy ,vec2(122.9898,783.233))) * 43758.5453);
            float y = fract(sin(dot(co.xy ,vec2(457.6537,537.2793))) * 37573.5913);
            return vec2(x,y);
        }
        vec3 SnowSingleLayer(vec2 uv,float layer){
            vec3 acc = vec3(0.3);
            uv = uv * (2.0+layer);
            float xOffset = uv.y * (((Hash11(layer)*2.-1.)*0.5+1.)*XSPEED);
            float yOffset = (YSPEED*time);
            uv += vec2(xOffset,yOffset);
            vec2 rgrid = Hash22(floor(uv)+(31.1759*layer));
            uv = fract(uv);
            uv -= (rgrid*2.-1.0) * 0.35;
            uv -=0.5;
            float r = length(uv);
            float circleSize = 0.08*(1.0+0.3*sin(time*SIZE_RATE));
            float val = smoothstep(circleSize,-circleSize,r);
            vec3 col = vec3(val,val,val)* rgrid.x ;
            return col;
        }

        void main()
        {
            time = czm_frameNumber / 120.0;
            vec3 col = vec3(0.3, .3, .3);
            // Normalized pixel coordinates (from 0 to 1)
            vec2 uv = gl_FragCoord.xy/czm_viewport.zw;
            uv *= vec2(czm_viewport.z/czm_viewport.w,1.0);
            vec3 acc = vec3(0,0,0);
            for (float i=0.;i<LAYERS;i++) {
                acc += SnowSingleLayer(uv,i); 
            }
            out_FragColor = mix( texture(colorTexture, v_textureCoordinates), vec4(acc,1.0) , 0.5);
        }

        `,
    });
    snowEffect.enabled = false;
    viewer.scene.postProcessStages.add(snowEffect);

    rainEffect = new Cesium.PostProcessStage({
        fragmentShader: `
        uniform sampler2D colorTexture;
        in vec2 v_textureCoordinates;
        float hash(float x){
            return fract(sin(x*23.3)*13.13);
        }
        void main(){
            float time = czm_frameNumber / 120.0;
            vec2 resolution = czm_viewport.zw;
            vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y);
            vec3 c=vec3(.6,.7,.8);
            float a=-.4;
            float si=sin(a),co=cos(a);
            uv*=mat2(co,-si,si,co);
            uv*=length(uv+vec2(0,8.9))*.3+1.;
            float v=1.-sin(hash(floor(uv.x*100.))*2.);
            float b=clamp(abs(sin(20.*time*v+uv.y*(5./(2.+v))))-.95,0.,1.)*20.;
            c*=v*b;
            out_FragColor = mix(texture(colorTexture, v_textureCoordinates), vec4(c, 1), 0.5);
        }`,
    });
    rainEffect.enabled = false;
    viewer.scene.postProcessStages.add(rainEffect);
}

/**
 * 初始化场景事件
 */
const initEvent = () => {
    let _sn = 0;
    const updateScene = () => {
        const sd = Cesium.Cartesian3.normalize(viewer.scene.sun._boundingVolume.center, new Cesium.Cartesian3);
        const vd = Cesium.Cartesian3.normalize(viewer.camera.position, new Cesium.Cartesian3);
        const sn = parseFloat(Cesium.Cartesian3.dot(vd, sd).toFixed(3))
        if (sn === _sn) return false;
        // viewer.postProcessStages.bloom.enabled = false;
        // viewer.scene.postProcessStages.ambientOcclusion.enabled = true;
        const value = Cesium.Math.clamp(sn, 0.05, 2.0);
        if (imagerLayer)
            imagerLayer.brightness = value + 0.3;
        if (googleTileset)
            googleTileset.customShader.uniforms.u_lightColor.value =
                Cesium.Color.multiplyByScalar(Cesium.Color.fromCssColorString("#ffffff"), value, new Cesium.Color);
        if (sn < 0) {
            // viewer.postProcessStages.bloom.enabled = true;
            // viewer.scene.postProcessStages.ambientOcclusion.enabled = false;
        }
        _sn = sn;
    }
    viewer.scene.postRender.addEventListener(() => updateScene());
    viewer.camera.setView({
        destination: { x: -2411264.0506999483, y: 5392065.788032695, z: 2404130.5345600643 },
        orientation: {
            heading: Cesium.Math.toRadians(73.17599066543983),
            pitch: Cesium.Math.toRadians(-14.283769462808346),
            roll: Cesium.Math.toRadians(0.006227514695949802)
        }
    })
    viewer.clock.currentTime = Cesium.JulianDate.fromIso8601("2023-04-14T04:00:00Z");
    const closeEffect = () => {
        snowEffect.enabled = false;
        rainEffect.enabled = false;
        googleTileset.customShader.uniforms.u_snowAlpha.value = 0.;
        googleTileset.customShader.uniforms.u_rainAlpha.value = 0.;

    }
    setTimeout(() => {
        closeLoading();
        showUI();

        document.getElementById("view").onclick = function () {
            closeEffect();
            viewer.scene.globe.show = false;
            viewer.camera.flyTo({
                destination: { x: -3018599.8547373875, y: 4933800.6373264985, z: 2683549.3590823035 },
                orientation: {
                    heading: Cesium.Math.toRadians(122.99145068799807),
                    pitch: Cesium.Math.toRadians(-23.795390458891127),
                    roll: Cesium.Math.toRadians(0.0011079070749852415)
                },
                duration: 6,
                easingFunction: Cesium.EasingFunction.CIRCULAR_OUT,
                complete: () => {
                    setTimeout(() => {
                        viewer.camera.flyTo({
                            destination: { x: 1329597.4708824896, y: -4661613.093307401, z: 4137106.7058978938 },
                            orientation: {
                                heading: Cesium.Math.toRadians(53.26771563981157),
                                pitch: Cesium.Math.toRadians(-28.91764530646933),
                                roll: Cesium.Math.toRadians(359.99632942190806)
                            },
                            duration: 6,
                            easingFunction: Cesium.EasingFunction.CIRCULAR_OUT,
                            complete: () => {
                                setTimeout(() => {
                                    viewer.camera.flyTo({
                                        destination: { x: -2418077.1476918706, y: 5386854.353530804, z: 2408511.7725730916 },
                                        orientation: {
                                            heading: Cesium.Math.toRadians(180.77344403307322),
                                            pitch: Cesium.Math.toRadians(-29.413972355525768),
                                            roll: Cesium.Math.toRadians(0.006019668414439345)
                                        },
                                        duration: 5,
                                        easingFunction: Cesium.EasingFunction.CIRCULAR_OUT,
                                        complete: () => {
                                            viewer.scene.globe.show = true;
                                        }
                                    })
                                }, 6000)
                            }
                        });
                    }, 6000)
                }
            });

        }
        document.getElementById("snow").onclick = function () {
            rainEffect.enabled = false;
            googleTileset.customShader.uniforms.u_rainAlpha.value = 0.;
            snowEffect.enabled = !snowEffect.enabled;
            let interval;
            if (snowEffect.enabled) {
                interval = setInterval(() => {
                    if (googleTileset.customShader.uniforms.u_snowAlpha.value >= 1.0) {
                        window.clearInterval(interval);
                        return false;
                    }
                    googleTileset.customShader.uniforms.u_snowAlpha.value += 0.01;
                }, 20)
            } else
                googleTileset.customShader.uniforms.u_snowAlpha.value = 0., window.clearInterval(interval);
        }
        document.getElementById("rain").onclick = function () {
            snowEffect.enabled = false;
            googleTileset.customShader.uniforms.u_snowAlpha.value = 0.;
            rainEffect.enabled = !rainEffect.enabled;
            let interval;
            if (rainEffect.enabled) {
                interval = setInterval(() => {
                    if (googleTileset.customShader.uniforms.u_rainAlpha.value >= 0.5) {
                        window.clearInterval(interval);
                        return false;
                    }
                    googleTileset.customShader.uniforms.u_rainAlpha.value += 0.05;
                }, 20)
            } else
                googleTileset.customShader.uniforms.u_rainAlpha.value = 0., window.clearInterval(interval);
        }
    }, 5000);

}


const main = async () => {
    showLoading();
    initViewer();
    await initScene();
    initEvent();
};


new window.WOW().init();
function showUI() {
    $("body").append(`
    <div id="topUI" class="topUI wow bounceInDown" data-wow-duration="3.0s">
            <span>实景城市</span>
            <a class="bvideo" href="https://space.bilibili.com/432028432">可视化场景分享</a>
            <a class="view" id="view" href="#">视图切换</a>
            <a class="snow" id="snow" href="#">雪天动画</a>
            <a class="rain" id="rain" href="#">雨天动画</a>
    </div>`);
}
function closeLoading() {
    $("#loadingIndicator").hide();
    $("#loadingIndicator2").hide();
}

function showLoading() {
    $("#loadingIndicator").show();
    $("#loadingIndicator2").show();
}

window.onload = main;